#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <strings.h>

int main(int argc, char** argv) {
	int sock;
	struct sockaddr_in addr;
	char send_buf[1024];
	char recv_buf[1024];
	
	sock = socket(AF_INET, SOCK_STREAM, 0);
	if (sock < 0) {
		perror("initialization of socket failed");
		exit(errno);
	}
	
	bzero(&addr, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(6006);
	addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
	
	inet_aton(argv[1], (struct in_addr*)&addr.sin_addr.s_addr);
	if(connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
		perror("connection failed");
		exit(errno);
	}
	while (1) {
		scanf("%s\n", send_buf);
		send(sock, send_buf, sizeof(send_buf), 0);
		recv(sock, recv_buf, sizeof(send_buf), 0);
		printf("%s\n", recv_buf);
	}
	close(sock);
	return 0;
}
